package export.microshop
{
	import com.dusk.util.TextUtil;
	import com.dusk.util.UtilBase;
	import com.game.data.vo.EquipmentVO;
	import com.game.data.vo.PlayerDataVO;
	import com.game.module.backpack.view.PackThings;
	
	import config.Config;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.TextField;
	
	public class ShopThing extends Sprite
	{
		
		public function ShopThing()
		{
			buyObj = {
				"thingnum": 0,
				"allmoney": 0
			};
			super();
			TextUtil.redrawTxtOld(this, needRedrawText);
			gc = Config.getIns();
			txt_name.selectable = false;
			txt_price.selectable = false;
			addEventListener(Event.ADDED_TO_STAGE, added, false, 0, true);
			addEventListener(Event.REMOVED_FROM_STAGE, removed, false, 0, true);
		}
		
		public var btn_buy:SimpleButton;
		public var txt_name:TextField;
		public var txt_price:TextField;
		public var txt_num:TextField;
		public var btn_up:SimpleButton;
		public var btn_down:SimpleButton;
		public var bgmc:Sprite;
		private var gc:Config;
		private var packThing:PackThings;
		private var thisEquipment:EquipmentVO;
		private var player:PlayerDataVO;
		private var amoney:uint;
		private var buyObj:Object;
		private var needRedrawText:Array = ["txt_name", "txt_num", "txt_price"];
		private var buyclick:Boolean;
		
		public function setEquipment(param1:EquipmentVO):void
		{
			setPlayer();
			setMoney();
			if (packThing == null)
			{
				packThing = new PackThings();
				packThing.x = 15;
				packThing.y = 20;
				addChild(packThing);
			}
			thisEquipment = param1;
			buyObj.thingnum = 1;
			var _loc3_:int = int(Micropayment.getSellPrice(thisEquipment.equipId));
			buyObj.allmoney -= _loc3_;
			visibleTrue();
			packThing.setEquipVO(thisEquipment, getPlayerId());
		}
		
		public function clearEquipment():void
		{
			if (Boolean(packThing) && contains(packThing))
			{
				buyclick = false;
				packThing.removeEquipIconAndNotPutInBack();
				thisEquipment = null;
				removeChild(packThing);
				packThing = null;
				if (buyObj.thingnum != 1)
				{
					buyObj.thingnum = 1;
				}
			}
		}
		
		public function buySuccess():void
		{
			buyclick = false;
			buyObj.thingnum = 1;
			txt_num.text = buyObj.thingnum + "";
		}
		
		public function getNumAndEquip():Object
		{
			var _loc1_:* = {
				"thingnum": 0,
				"equip"   : null
			};
			if (buyclick)
			{
				_loc1_.thingnum = buyObj.thingnum;
				_loc1_.equip = thisEquipment;
			}
			return _loc1_;
		}
		
		public function getMoneySuccess():void
		{
			setMoney();
			txt_num.text = buyObj.thingnum + "";
		}
		
		private function visibleFalse():void
		{
			txt_name.text = "";
			txt_price.text = "";
			btn_buy.visible = false;
			bgmc.visible = false;
			btn_up.visible = false;
			btn_down.visible = false;
			txt_num.text = "0";
			txt_num.visible = false;
		}
		
		private function visibleTrue():void
		{
			var _loc1_:int = int(Micropayment.getSellPrice(thisEquipment.equipId));
			txt_name.text = thisEquipment.name;
			txt_num.text = buyObj.thingnum + "";
			txt_price.text = _loc1_ + "灵魂";
		}
		
		private function setPlayer():void
		{
			if (parent)
			{
				player = Micropayment(parent).getUser();
			}
		}
		
		private function setMoney():void
		{
			if (parent)
			{
				buyObj.allmoney = Micropayment(parent).getAllMoney();
				amoney = buyObj.allmoney;
			}
		}
		
		private function changePlayer(param1:*):void
		{
			setPlayer();
		}
		
		private function buychange(param1:*):void
		{
			buyclick = false;
		}
		
		private function getPlayerId():uint
		{
			return player.getControlPlayer();
		}
		
		private function added(param1:Event):void
		{
			btn_buy.addEventListener(MouseEvent.CLICK, buyClick);
			btn_up.addEventListener(MouseEvent.CLICK, upClick);
			btn_down.addEventListener(MouseEvent.CLICK, downClick);
			gc.eventManager.addEventListener("ChangePlayer", changePlayer);
			gc.eventManager.addEventListener("BUYCHANGLE", buychange);
			txt_num.addEventListener(Event.CHANGE, changenum);
			txt_num.restrict = "0-9";
		}
		
		private function removed(param1:Event):void
		{
			btn_buy.removeEventListener(MouseEvent.CLICK, buyClick);
			btn_up.removeEventListener(MouseEvent.CLICK, upClick);
			btn_down.removeEventListener(MouseEvent.CLICK, downClick);
			gc.eventManager.removeEventListener("ChangePlayer", changePlayer);
			gc.eventManager.addEventListener("BUYCHANGLE", buychange);
			txt_num.removeEventListener(Event.CHANGE, changenum);
		}
		
		private function changenum(param1:Event):void
		{
			buyObj.thingnum = txt_num.text;
		}
		
		private function buyClick(param1:MouseEvent):void
		{
			if (buyObj.thingnum > 0)
			{
				buyclick = true;
				new BuyThingCmd(amoney, Micropayment.getSellPrice(thisEquipment.equipId), buyObj.thingnum, thisEquipment.name, thisEquipment.color.toString(), player);
			}
		}
		
		private function upClick(param1:MouseEvent):void
		{
			if (buyObj.thingnum > 99)
			{
				return;
			}
			buyObj = UtilBase.clone(buyObj);
			buyObj.allmoney -= Micropayment.getSellPrice(thisEquipment.equipId);
			++buyObj.thingnum;
			txt_num.text = buyObj.thingnum + "";
		}
		
		private function downClick(param1:MouseEvent):void
		{
			if (buyObj.thingnum > 1)
			{
				buyObj = UtilBase.clone(buyObj);
				buyObj.allmoney += Micropayment.getSellPrice(thisEquipment.equipId);
				--buyObj.thingnum;
			}
			txt_num.text = buyObj.thingnum + "";
		}
	}
}
